package company

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"jwgin/model/mysql"
	"jwgin/service"
)

type whois struct {
}

func (l *whois) Routers(Router *gin.RouterGroup) {
	Router.GET("whois", l.get)   //列表
	Router.POST("whois", l.post) //更新
}

func (l *whois) get(c *gin.Context) {
	UserID := c.GetUint("UserID")
	if UserID == 0 {
		RespErr(c, fmt.Errorf("系统用户认证失败"))
		return
	}
	resp := &mysql.Whois{}
	service.G.Model(mysql.Whois{}).Where("comid = ?", UserID).First(resp)
	RespSucc(c, resp)
}
func (l *whois) post(c *gin.Context) {
	q := &mysql.Whois{}
	if err := c.ShouldBind(q); err != nil {
		RespErr(c, err)
		return
	}

	UserID := c.GetUint("UserID")
	if UserID == 0 {
		RespErr(c, fmt.Errorf("系统用户认证失败"))
		return
	}

	q.Comid = int(UserID)

	who := &mysql.Whois{}
	err := service.G.Table(new(mysql.Whois).TableName()).Where("comid =  ? ", q.Comid).First(who).Error

	if err == gorm.ErrRecordNotFound {
		service.G.Save(q)
		RespSucc(c, nil)
	} else {
		service.G.Table(new(mysql.Whois).TableName()).Where("comid =  ? ", q.Comid).Updates(q)
		RespSucc(c, nil)
	}

}
